#include <vector>
#include <map>
#include <string>

using namespace std;

class Solution
{
public:
    vector<string> topKFrequent(vector<string> &words, int k)
    {
        vector<string> ret;
        map<string, int> dic;
        for (auto &e : words)
        {
            ++dic[e];
        }

        while (k--)
        {
            auto h = dic.rbegin();
            int max = 0;
            string tmp;
            while (h != dic.rend())
            {
                if (h->second >= max)
                {
                    tmp = h->first;
                    max = h->second;
                }
                h++;
            }
            ret.push_back(tmp);
            dic.erase(tmp);
        }
        return ret;
    }
};